package com.cino.users.controller;


import com.cino.basic.base.exception.BusinessException;
import com.cino.basic.util.AjaxResult;
import com.cino.users.dto.AccountDto;
import com.cino.users.service.LogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LogininfoService logininfoService;

    @PostMapping(value = "/account")
    public AjaxResult accountLogin(@RequestBody AccountDto accountDto){
        try {
            Map<String, Object> map = logininfoService.loadAccount(accountDto);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setObj(map);
            return ajaxResult;
        }catch (BusinessException e) {
           return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试");
        }
    }

    @PostMapping(value = "/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatLogin(map);
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试");
        }
    }

    @PostMapping(value = "/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            AjaxResult ajaxResult = logininfoService.wechatBinder(map);
            return ajaxResult;
        }catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试");
        }
    }

}
